package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.SentMessages;
import com.example.mall.dto.SentMessagesDto;
import com.example.mall.mapper.SentMessagesMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.SentMessagesQuery;
import com.example.mall.service.ISentMessagesService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 发送消息表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class SentMessagesServiceImpl extends ServiceImpl<SentMessagesMapper, SentMessages> implements ISentMessagesService {

    @Autowired
    private SentMessagesMapper sentMessagesMapper;

    @Override
    public PageDTO<SentMessagesDto> queryPage(SentMessagesQuery sentMessagesQuery) {
        if (sentMessagesQuery == null) {
            sentMessagesQuery = new SentMessagesQuery();
        }
        Page<SentMessages> page = sentMessagesQuery.toMpPageByDefaultCreateTimeAsc();
        Page<SentMessages> cartPage = lambdaQuery()
                .like(sentMessagesQuery.getSentId()!= null, SentMessages::getSentId, sentMessagesQuery.getSentId())
                .like(sentMessagesQuery.getMessageId()!= null, SentMessages::getMessageId, sentMessagesQuery.getMessageId())
                .like(sentMessagesQuery.getBody()!= null, SentMessages::getBody, sentMessagesQuery.getBody())
                .like(sentMessagesQuery.getTopic()!= null, SentMessages::getTopic, sentMessagesQuery.getTopic())
                .like(sentMessagesQuery.getTag()!= null, SentMessages::getTag, sentMessagesQuery.getTag())
                .like(sentMessagesQuery.getSendStatus()!= null, SentMessages::getSendStatus, sentMessagesQuery.getSendStatus())
                .like(sentMessagesQuery.getUserId()!= null, SentMessages::getUserId, sentMessagesQuery.getUserId())
                .like(sentMessagesQuery.getOrderId()!= null, SentMessages::getOrderId, sentMessagesQuery.getOrderId())
                .like(sentMessagesQuery.getDescription()!= null, SentMessages::getDescription, sentMessagesQuery.getDescription())
                .apply(sentMessagesQuery.getSendTime() != null,
                        "DATE_FORMAT(send_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        sentMessagesQuery.getSendTime())
                .orderByDesc(SentMessages::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, SentMessagesDto.class);
    }

    @Override
    public SentMessages querySentMessagesById(Long sentId) {
        AssertUtil.isNotNull(sentId, "sentId不能为空");
        return sentMessagesMapper.selectById(sentId);
    }

    @Override
    public List<SentMessages> querySentMessagesList() {
        return this.list();
    }

    @Override
    public void insert(SentMessagesDto sentMessagesDto) {
        SentMessages sentMessages = new SentMessages();
        CopyUtils.copyProperties(sentMessagesDto, sentMessages);
        sentMessages.setSentId(CodeGenerateUtils.generateUniqueId());
        sentMessages.setCreateDate(new Date());
        sentMessages.setUpdateDate(new Date());
        sentMessages.setSendStatus(BaseConstants.NOT_USED);
        sentMessagesMapper.insert(sentMessages);
    }

    @Override
    public void update(SentMessagesDto sentMessagesDto) {
        AssertUtil.isNotNull(sentMessagesDto.getSentId(), "sentId不能为空");
        SentMessages sentMessages = sentMessagesMapper.selectById(sentMessagesDto.getSentId());
        AssertUtil.isNotNull(sentMessages, "sentId不存在");
        CopyUtils.copyProperties(sentMessagesDto, sentMessages);
        sentMessages.setUpdateDate(new Date());
        sentMessagesMapper.updateById(sentMessages);
    }

    @Override
    public void delete(Long sentId) {
        AssertUtil.isNotNull(sentId, "sentId不能为空");
        SentMessages sentMessages = sentMessagesMapper.selectById(sentId);
        AssertUtil.isNotNull(sentMessages, "sentId不存在");
        sentMessages.setUpdateDate(new Date());
        sentMessages.setSendStatus(BaseConstants.EXPIRED);
        sentMessagesMapper.updateById(sentMessages);
    }

    @Override
    public void deleteBatch(List<Long> sentIds) {
        AssertUtil.isNotNull(sentIds, "sentIds不能为空");
        List<SentMessages> sentMessages = sentMessagesMapper.selectBatchIds(sentIds);
        AssertUtil.isNotNull(sentMessages, "sentIds不存在");
        for (SentMessages sentMessage : sentMessages) {
            sentMessage.setUpdateDate(new Date());
            sentMessage.setSendStatus(BaseConstants.EXPIRED);
            sentMessagesMapper.updateById(sentMessage);
        }
    }
}
